package net.hzlj.platform.enterprise.utils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.support.annotation.NonNull;
import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.google.gson.GsonBuilder;

import net.grandcentrix.tray.AppPreferences;
import net.grandcentrix.tray.TrayPreferences;
import net.grandcentrix.tray.core.TrayStorage;
import net.hzlj.platform.enterprise.common.Constant;


/**
 * @Description:
 * @Author: Jason Zhang
 * @Date: 2017/2/27 0027
 */

public class AppPreferencesUtils {

    private static final String TAG = AppPreferencesUtils.class.getSimpleName();

    private static final String MODULE_NAME = Constant.PROJECT_NAME;
    private static final int MODULE_VERSION = 1;

    private static AppTrayPreferences mAppTrayPreferences;

    private static class AppTrayPreferences extends AppPreferences {

        public AppTrayPreferences(Context context) {
            super(context);
        }
    }
    private static AppTrayPreferences getPreferences(Context context){
        if (mAppTrayPreferences == null) {
            synchronized(AppPreferencesUtils.class) {
                if (mAppTrayPreferences == null) {
                    mAppTrayPreferences = new AppTrayPreferences(context);
                }
            }
        }
        return mAppTrayPreferences;
    }

    public static boolean putString(Context context, String key, String value) {
        return getPreferences(context).put(key,value);
    }

    public static String getString(Context context, String key, String defaultValue) {
        return getPreferences(context).getString(key,defaultValue);
    }

    public static boolean putBoolean(Context context, String key, boolean value) {
        Log.i(TAG,"putBoolean key:" + key + ":" + value);
        return getPreferences(context).put(key,value);
    }

    public static boolean getBoolean(Context context, String key, boolean defaultValue) {
        return getPreferences(context).getBoolean(key,defaultValue);
    }

    public static boolean putInt(Context context, String key, int value) {
        return getPreferences(context).put(key,value);
    }

    public static int getInt(Context context, String key, int defaultValue) {
        return getPreferences(context).getInt(key, defaultValue);
    }

    public static boolean putFloat(Context context, String key, float value) {
        return getPreferences(context).put(key, value);
    }

    public static float getFloat(Context context, String key, float defaultValue) {
        return getPreferences(context).getFloat(key, defaultValue);
    }

    public static boolean putLong(Context context, String key, long value) {
        return getPreferences(context).put(key,value);
    }

    public static long getLong(Context context, String key, long defaultValue) {
        return getPreferences(context).getLong(key, defaultValue);
    }

    public static <T> boolean putObject(Context context, String key, T o) {
        if (o == null){
            return false;
        }
        return putString(context,key, JSON.toJSONString(o));
    }

    public static <T> T getObject(Context context, String key,Class<T> cla) {
        String content = getString(context,key,null);
        if (StringUtils.isNotEmpty(content)){
            return new GsonBuilder().create().fromJson(
                    content, cla);
        }else {
            return null;
        }

    }
}
